<?php

	class Menu{
		
		public $id;		
		public $descMenu;		
		public $descHTML;
		public $subMenusAcesso;
		
		public function getId(){
			
			return $this->id;
			
		}
		
		public function setId($id){
			
			$this->id = $id;
			
		}
		
		public function getDescMenu(){
			
			return $this->descMenu;
			
		}
		
		public function setDescMenu($descMenu){
			
			$this->descMenu = $descMenu;
			
		}
		
		public function getDescHTML(){
			
			return $this->descHTML;
			
		}
		
		public function setDescHTML($descHTML){
			
			$this->descHTML= $descHTML;
			
		}
		
		public function setSubMenusAcesso($subMenusAcesso){
			$this->subMenusAcesso = $subMenusAcesso;
		}
		
		public function getSubMenusAcesso(){
			return $this->subMenusAcesso;
		}
		
		public function inserir(){
						
				$db = new conexao();
				$db->abrir();
				
				$sql = "INSERT INTO `menu` ( `men_desc`, `men_html`) VALUES 
	  					('".$this->getDescMenu()."','".$this->getDescHTML()."');";
	  			
			 	if($db->inserir($sql)){
					
					$this->setId($db->get_ult_id());
					
					$db->fechar();
					return true;
					
				}
				
				else{
					
					$db->fechar();
					return false;
					
				}
		
		}
		
		public function alterar(){
			
			$db = new conexao();
			$db->abrir();
			
			$sql = "update `menu` set `men_des` = '".utf8_decode($this->getDescMenu())."', `men_html` = '".$this->getDescHTML()."'
					where `men_id` = '".$this->getId()."';";
			
			if($db->consulta($sql)){
				
				$db->fechar();
				return true;
					
			}
			else{
				
				$db->fechar();
				return false;
				
			}
			
		}
		
		public function getSubMenuForId($idUsuario){
			$db = new conexao();
			$db->abrir();
			$sql = "select distinct sub.*,ur.* from `menu` me
			inner join  `sub_menu` sub on
			sub.`men_id` = me.`men_id`
			inner join  `tipo_usuario_submenu`tps on
			sub.`sub_id` = tps.`sub_id`
			inner join `tipo_usuario` tpu on
			tpu.`tip_use_id` = tps.`tip_use_id`
			inner join `url` ur
			on ur.`sub_id`  = sub.`sub_id`
			where tpu.`tip_use_id` = ".$idUsuario."
			and sub.`men_id` = ".$this->getId();
			
			$resp = $db->consulta($sql);
			$subMenuArry =  array();
			while($linha= $db->get_linha($resp)){
					$subMenu = new SubMenu();
					$subMenu->setId($linha["sub_id"]);
					$subMenu->setDescHtml($linha["sub_html"]);
					$subMenu->setUrlIcone($linha["sub_urlimg"]);
					$subMenuArry[] = $subMenu;
			} 
			$this->setSubMenusAcesso($subMenuArry);
			
		}
		public function verificaMenuExistente(){
			
			$db = new conexao();
			$db->abrir();
			$sql ="select count(*) as quant from menu where men_desc ='".$this->getDescMenu()."';";
			$result = $db->consulta($sql);
			$resp = 0;
			while($linha = $db->get_linha($result)){
				$resp = $linha["quant"];
			}
			$db->fechar();
			return $resp;
		}
		
		public function selecionar(){
	
				$db = new conexao();
				$db->abrir();
				
				$sql = "SELECT *FROM menu where men_id = ".$_GET['id']."";
								
				return $sql;
		}
		public function selectBox($nome,$idNome){
			
			$gerentHTML = new GerenteHTML();
			return $gerentHTML->selectBox("menu","men_desc","men_id",$nome,$idNome);
			
		}
		
		public function excluir() {
			
			$db = new conexao();
			$db->abrir();
			
			$sql = "DELETE FROM `menu` WHERE `men_id` = ".$_GET['id']."";
			
			if($db->consulta($sql)){
				
				$db->fechar();
				return true;
					
			}
			else{
				
				$db->fechar();
				return false;
				
			}
			
		}
		
		public function selectBoxSet($codigo=""){

			$consultar = "";
			$sql =" select *from `menu` order by `menu`.`men_desc`";

			$db = new conexao();
			$db->abrir();

			$resultado = $db->consulta($sql);

			while($linha = $db->get_linha($resultado)){

				$consultar.="<option value = ".$linha['men_id']." ";

				if($codigo!="" && $codigo == $linha["men_id"]){

					$consultar.=" selected ";

				}

				$consultar.=">";

				$consultar.=$linha['men_desc']."</option>";

			}
			
			$db->fechar();
			return $consultar;
			
        }
		
	}
?>
